<template>
  <div>
    <el-header class="el-header2" height="35px">
      <el-breadcrumb
        style="margin-left: 10px"
        separator-class="el-icon-arrow-right"
      >
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        <el-breadcrumb-item>添加商品</el-breadcrumb-item>
      </el-breadcrumb>
    </el-header>

    <el-card>
      <!-- 提示区域 -->
      <el-alert :closable="false" title="添加商品" type="info" center>
      </el-alert>

      <!-- 步骤区域 -->
      <el-steps
        style="margin-top: 50px"
        align-center
        :active="activeIndex - 0"
        finish-status="success"
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>

      <!-- 侧边栏目 -->
      <!-- tabs -->
      <el-form
        :model="addForm"
        :rules="addFormrules"
        ref="addFormref"
        label-width="100px"
        label-position="top"
      >
        <el-tabs
          @tab-click="tabonclick"
          :before-leave="leave"
          :tab-position="'left'"
          v-model="activeIndex"
        >
          <!-- 基本信息 -->
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="addForm.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="goods_price">
              <el-input type="number" v-model="addForm.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品重量" prop="goods_weight">
              <el-input v-model="addForm.goods_weight"></el-input>
            </el-form-item>
            <el-form-item label="商品数量" prop="goods_number">
              <el-input v-model="addForm.goods_number" type="number"></el-input>
            </el-form-item>

            <el-form-item label="商品分类" prop="goods_cat">
              <el-cascader
                expand-trigger="hover"
                :clearable="true"
                v-model="cascaderselectkey"
                :options="list"
                :props="cascaderprops"
                @change="cascaderChange"
                clearCheckedNodes
                :checkStrictly="true"
                placeholder="默认为最高级分类"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="1">
            <div style="margin-top: 20px"></div>
            <el-input
              type="textarea"
              placeholder="请输入内容"
              v-model="addForm.goods_introduce"
              maxlength="30"
              show-word-limit
            >
            </el-input>
            <div style="margin-top: 20px"></div>
            <el-button type="primary" @click="send">添加商品</el-button>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import _ from "lodash";

export default {
  data() {
    return {
      // 分类列表
      list: [],
      //   级联树形
      cascaderprops: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
        checkStrictly: true,
      },
      cascaderselectkey: [],

      //添加的商品的date
      addForm: {
        goods_name: "1",
        goods_price: "1",
        goods_number: "1",
        goods_weight: "1",
        //商品所属的 分类数组
        goods_cat: [],
        //商品介绍
        goods_introduce: "商品介绍",
      },
      // 规则
      addFormrules: {
        goods_name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        goods_number: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        goods_cat: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },

      // 步骤条在哪一页
      activeIndex: "0",
    };
  },
  async created() {
    this.resetPage();
  },
  methods: {
    async resetPage() {
      await this.getList0();
      await this.getList1();
      await this.list1AddtoList0();
      console.log(this.list);
    },
    async getList0() {
      const { data } = await this.axios({
        method: "get",
        url: "/goods/getList0",
      });
      // console.log(data);
      this.list = data;
      console.log(this.list);
      return data;
      // console.log(this.list);
    },
    async getList1() {
      const { data: res1 } = await this.axios({
        method: "get",
        url: "/goods/getList1",
      });
      this.List1 = res1;
    },
    list1AddtoList0() {
      this.list.forEach((item0, index) => {
        item0.children = [];
        item0.disabled = "true";
        this.List1.forEach((item1) => {
          if (item1.cat_pid === item0.cat_id) {
            // item1.disabled = "true";
            item0.children.push(item1);
          }
        });

        this.$set(this.list, index, item0);
      });
    },

    // 级联改变时候
    cascaderChange() {
      this.addForm.goods_cat = this.cascaderselectkey;
      console.log(this.addForm);
    },
    // 标签页改变时候 跳转到哪个页面
    tabonclick() {},
    // 发送商品表单
    async send() {
      const form = _.cloneDeep(this.addForm);
      form.goods_cat = form.goods_cat.join(",");
      console.log(form, 1);
      this.$refs.addFormref.validate((valid) => {
        if (!valid) {
          return this.$message.error("请填写完整商品数据");
        }
      });
      form.goods_cat = form.goods_cat.split(",");
      console.log(form);
      const { data } = await this.axios({
        method: "post",
        data: form,
        url: "/goods/addGood",
      });
      // console.log(data);
      if (data.meta.status === 200) {
        this.$router.push("/home/goodslist");
        return this.$message.success("增加商品成功");
      } else return this.$message.error("增加商品失败");
    },
  },
};
</script>

<style lang="scss" scoped>
.el-checkbox {
  margin: 0 10px 0 0;
}
.send {
  margin-top: 15px;
}
</style>
